package com.eve.api.client;

import java.util.List;

import com.eve.api.shared.ApiCharacterProxy;
import com.eve.api.shared.ApiCharacterRequest;
import com.eve.api.shared.DataRequestFactory;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.Style.Overflow;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.resources.client.CssResource.NotStrict;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.web.bindery.event.shared.EventBus;
import com.google.web.bindery.event.shared.SimpleEventBus;
import com.google.web.bindery.requestfactory.shared.Receiver;
import com.google.web.bindery.requestfactory.shared.Request;

// Entry point class.
public class EveApiViewer implements EntryPoint {
	
	private List<ApiCharacterProxy> characterList;
	
	interface Binder extends UiBinder<DockLayoutPanel, EveApiViewer> { }

	interface GlobalResources extends ClientBundle {
		@NotStrict
	    @Source("global.css")
	    CssResource css();
	}

	private static final Binder binder = GWT.create(Binder.class);

	@UiField TopPanel topPanel;
	@UiField CharacterDetail mailDetail;
	@UiField Shortcuts shortcuts;
	
	private final APIRequestServiceAsync apiRequestService = GWT
			.create(APIRequestService.class);

	// This is the entry point method.
	public void onModuleLoad() {
		apiRequestService.getCharacters(new AsyncCallback<String>() {

			@Override
			public void onFailure(Throwable caught) {

			}

			@Override
			public void onSuccess(String result) {
				final EventBus eventBus = new SimpleEventBus();
				DataRequestFactory requestFactory = GWT.create(DataRequestFactory.class);
				requestFactory.initialize(eventBus);
				ApiCharacterRequest request = requestFactory.characterRequest();
				Request<List<ApiCharacterProxy>> charRequest = request.getCharacters();
				charRequest.fire( new Receiver<List<ApiCharacterProxy>>() {
					public void onSuccess(List<ApiCharacterProxy> response) {
						characterList = response;
						initCharacters();
					}
				});
			}
			
		});
		
		// Inject global styles.
	    GWT.<GlobalResources>create(GlobalResources.class).css().ensureInjected();

	    // Create the UI defined in Mail.ui.xml.
	    DockLayoutPanel outer = binder.createAndBindUi(this);

	    // Get rid of scrollbars, and clear out the window's built-in margin,
	    // because we want to take advantage of the entire client area.
	    Window.enableScrolling(false);
	    Window.setMargin("0px");

	    // Special-case stuff to make topPanel overhang a bit.
	    Element topElem = outer.getWidgetContainerElement(topPanel);
	    topElem.getStyle().setZIndex(2);
	    topElem.getStyle().setOverflow(Overflow.VISIBLE);

	    // Add the outer panel to the RootLayoutPanel, so that it will be
	    // displayed.
	    RootLayoutPanel root = RootLayoutPanel.get();
	    root.add(outer);
	}
	
	private void initCharacters() {
		for(ApiCharacterProxy character : characterList) {
			shortcuts.characters.addCharacter(character.getCharacterID());
		}
	}
}
